/**
 * 
 */
package PVPSound;

import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.table.AbstractTableModel;


/**
 * 
 */
public class KillTableModel extends AbstractTableModel {
	
	private ArrayList<String> names = new ArrayList<String>();
	private HashMap<String, Integer> kills = new HashMap<String, Integer> ();

	
	/**
	 * 
	 */
	public KillTableModel() {
		super();
	}
	
	public void clear () {
		
		synchronized (names) {
			names.clear();
			kills.clear();
		}
		
		fireTableDataChanged();	
	}
	
	
	public void kill (String name) {
		
		synchronized (names) {
			
			int before = 1;
			
			if (names.contains(name))
				before = kills.get(name) + 1;
			else 
				names.add(name);
			
			kills.put(name, before);				
		}
		
		fireTableDataChanged();	
		
	}	
	
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return kills.size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return 2;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		
		synchronized (names) {		
		
			if (columnIndex == 0) {
				return names.get(rowIndex);
			} else
				return kills.get(names.get(rowIndex));			
		}
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return columnIndex == 0 ? String.class : Integer.class;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	@Override
	public final String getColumnName(int column) {
		return column == 0 ? "Enemy" : "Frags";
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}
	
	
}
